<?php

use yii\helpers\Html;
use yii\widgets\Pjax;
use yii\helpers\Url;

/* @var $this yii\web\View */
/* @var $searchModel frontend\modules\resource\models\HyBookSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = '资源管理';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="hy-book-index">
    <div class="hy-book-btn" style="margin: 10px 0;position: relative;">
        <button type="button" data-value="1" class="btn btn-resource  btn-primary btn-lg">图书类</button>
        <button type="button" data-value="2" class="btn btn-resource  btn-success btn-lg">期刊类</button>
        <button type="button" data-value="3" class="btn btn-resource  btn-info btn-lg">图片类</button>
        <button type="button" data-value="4" class="btn btn-resource  btn-warning btn-lg">音频类</button>
        <button type="button" data-value="5" class="btn btn-resource  btn-danger btn-lg">视频类</button>
    </div>

    <?php Pjax::begin(['id' => 'centerView']); ?>
    <div class="searchBody">
        <?= $this->render('_search', ['model' => $searchModel]); ?>
        <!--功能按钮-->
        <div class="addBtn">
            <?= Html::a('新增资源', 'javascript:;', ['class' => 'btn btn-success create']) ?>
        </div>
    </div>
    <!--功能-->
    <div>
        <?= Html::button('删除资源', ['class' => 'btn btn-danger z-remove']) ?>
        <?= Html::button('批量推荐', ['class' => 'btn btn-primary recommend']) ?>
        <?= Html::button('批量首页', ['class' => 'btn btn-primary face']) ?>
        <?= Html::button('批量专题', ['class' => 'btn btn-primary topic']) ?>
    </div>

    <?= $this->render('../resource/_index_' . $type, ['model' => $searchModel, 'dataProvider' => $dataProvider]); ?>
    <?php Pjax::end(); ?>
</div>
<div id="open" style="display: none;">
    <div style="text-align: center;margin: 20px;display: flex;">
        <select id="hlspecial" class="form-control" name="" aria-required="true" style="width: 100%;">

        </select>
    </div>
    <div style="display: flex;justify-content: center;margin-top: 20px">
        <span id="add_special_resource" class='btn btn-primary' type="button">确认添加</span>
    </div>
</div>
<!--增删改 操作-->
<?php echo $this->render('../../../common/views/base/_column', ['option' => ['c', 'u']]); ?>
<script>
    <?php $this->beginBlock('js_ok_remove') ?>
    let ck = <?=$type?>;
    let down = true;
    $("button[data-value='" + ck + "']").addClass('checkedCate');
    //下载模板
    $(document).on('click', '.muban', function () {
        if (down) {
            down = !down;
            window.location.href = '<?=Url::toRoute('branch-resource/template')?>'+'?type=' + ck;
            down = !down;
        }
    });
    //切换分类
    $(document).on('click', '.btn-resource', function () {
        var id = $(this).attr('data-value');
        window.location.href = '<?=Url::toRoute('branch-resource/index')?>'+'?type=' + id;
    });
    //推荐
    $(document).on('click', '.recommend', function () {

        let id = allChecked();
        console.log(id);
        if (id) {
            layer.confirm('确认添加推荐资源？', {icon: 3, title: ''}, function (index) {
                $.ajax({
                    url: '<?=Url::toRoute('batch-support')?>',//'/resource/resource/upload-file'
                    type: 'POST',
                    data: {
                        id: JSON.stringify(id),
                        r_type: ck,
                        type: 2
                    },
                    dataType: "json",
                    success: function (msg) {

                        if (msg.status == 200) {
                            layer.msg('新增' + msg['data'] + '条推荐资源', {icon: 1, time: 1000}, function () {
                            });
                        } else {
                            layer.msg(msg['msg'], {icon: 2, time: 1000});
                        }
                    },
                    error: function () {
                    },
                });

                layer.close(index);
            });


        } else {
            layer.msg('先选择记录');
        }
    });
    //首页
    $(document).on('click', '.face', function () {
        console.log(11);
        let id = allChecked();
        if (id) {
            layer.confirm('确认添加首页资源？', {icon: 3, title: ''}, function (index) {
                $.ajax({
                    url: '<?=Url::toRoute('batch-support')?>',//'/resource/resource/upload-file'
                    type: 'POST',
                    data: {
                        id: JSON.stringify(id),
                        r_type: ck,
                        type: 1
                    },

                    dataType: "json",
                    success: function (msg) {

                        if (msg.status == 200) {
                            layer.msg('新增' + msg['data'] + '条首页资源', {icon: 1, time: 1000}, function () {
                            });
                        } else {
                            layer.msg(msg['msg'], {icon: 2, time: 1000});
                        }
                    },
                    error: function () {
                    }
                });
                layer.close(index);
            });
        } else {
            layer.msg('先选择记录');
        }
    });
    //移除
    $(document).on('click', '.z-remove', function () {

        let id = allChecked();
        if (id) {
            layer.confirm('确认删除资源？', {icon: 3, title: ''}, function (index) {
                $.ajax({
                    url: '<?=Url::toRoute('batch-remove')?>',//'/resource/resource/upload-file'
                    type: 'POST',
                    data: {
                        id: JSON.stringify(id),
                        r_type: ck,
                    },
                    dataType: "json",
                    success: function (msg) {

                        if (msg.status == 200) {
                            layer.msg(msg['msg'], {icon: 1, time: 1000});
                            $.pjax.reload({container: "#centerView"});
                        } else {
                            layer.msg(msg['msg'], {icon: 2, time: 1000});
                        }
                    },
                    error: function () {
                    }
                });
                layer.close(index);
            });
        } else {
            layer.msg('先选择记录');
        }
    });
    //专题列表
    $(document).on('click', '.topic', function () {
        let id = allChecked();
        if (id) {
            layer.open({
                type: 1,
                title: "专题列表",
                area: ["350px", "160px"],
                content: $('#open'),
            });
            //获取选项
            $.ajax({
                url: '<?=Url::toRoute('s-option')?>',//'/resource/resource/upload-file'
                type: 'GET',
                dataType: "json",
                success: function (msg) {

                    if (msg.status == 200) {
                        $('#hlspecial').html(msg.data);
                    }
                },
                error: function () {
                }
            });
        } else {
            layer.msg('先选择记录');
        }
    });
    //确认添加
    $(document).on('click', '#add_special_resource', function () {
        let id = allChecked();
        let sp_id = $('#hlspecial').val();
        if (id) {
            layer.confirm('确认添加专题资源？', {icon: 3, title: ''}, function (index) {
                $.ajax({
                    url: '<?=Url::toRoute('batch-special')?>',//'/resource/resource/upload-file'
                    type: 'POST',
                    data: {
                        id: JSON.stringify(id),
                        sp_id: sp_id,
                        r_type: ck,
                        type: 2
                    },
                    dataType: "json",
                    success: function (msg) {

                        if (msg.status == 200) {
                            layer.msg('添加成功', {icon: 1, time: 1000}, function () {
                            });
                        } else {
                            layer.msg(msg['msg'], {icon: 2, time: 1000});
                        }
                    },
                    error: function () {
                    },
                });

                layer.close(index);
            });
        }
    });

    //获取复选框id
    function allChecked() {

        var id = [];
        var dom = $('.grid-view tbody').find('input[type=checkbox]:checked');
        if (dom.length) {
            $.each(dom, function () {
                id.push($(this).val())
            });
        } else {
            return 0;
        }
        return id;
    }
    <?php $this->endBlock() ?>
</script>
<?php $this->registerJs($this->blocks['js_ok_remove'], \yii\web\View::POS_END); ?>
